﻿using FTBS.Models.BLO;
using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FTBS.Controllers
{
    public class TicketTypeController : Controller
    {
        TicketTypeBLO _ticketTypeBLO = new TicketTypeBLO();
        public ActionResult Index()
        {
            return View(_ticketTypeBLO.LoadAllTicketTypes());
        }

        //--------------Create--------------------------
        public ActionResult Create()
        {
            this.Session["TicketTypeName"] = null;
            return View();
        }

        [HttpPost]
        public ActionResult Create(TicketType ticketType)
        {
            if (ModelState.IsValid)
            {
                if (_ticketTypeBLO.CheckIsExisted(ticketType))
                {
                    this.Session["TicketTypeName"] = null;
                    _ticketTypeBLO.AddTicketType(ticketType);
                    return RedirectToAction("Index");
                }
                this.Session["TicketTypeName"] = null;
                return View(ticketType);
            }
            return View(ticketType);
        }
        //------------Edit------------------------------
        public ActionResult Edit(int tTypeId = 0)
        {
            TicketType ticketType = _ticketTypeBLO.LoadATicketType(tTypeId);
            if (ticketType == null)
            {
                return HttpNotFound();
            }
            this.Session["curName"] = ticketType.TicketTypeName;
            this.Session["TicketTypeName"] = null;
            return View(ticketType);

        }
        [HttpPost]
        public ActionResult Edit(TicketType ticketType)
        {
            if (ModelState.IsValid)
            {
                int compare = String.Compare(ticketType.TicketTypeName, (String)this.Session["curName"]);
                if (!_ticketTypeBLO.CheckIsExisted(ticketType) || (_ticketTypeBLO.CheckIsExisted(ticketType) && compare == 0))
                {
                    this.Session["TicketTypeName"] = null;
                    _ticketTypeBLO.UpdateTicketType(ticketType);
                    return RedirectToAction("Index");
                }
                else if (_ticketTypeBLO.CheckIsExisted(ticketType) && compare != 0)
                {
                    this.Session["TicketTypeName"] = ticketType.TicketTypeName;
                    return View(ticketType);
                }
            }
            return View(ticketType);
        }
        //------------------Delete---------------------
        [HttpPost]
        public ActionResult Delete(TicketType ticketType)
        {
            if (ModelState.IsValid)
            {
                _ticketTypeBLO.DeleteTicketType(ticketType);
                return RedirectToAction("Index");
            }
            return View(ticketType);
        }
    }
}
